package com.zmxstudy.rbac.controller;

import com.zmxstudy.rbac.base.BaseController;
import com.zmxstudy.rbac.entity.Role;
import com.zmxstudy.rbac.entity.UserAndRole;
import com.zmxstudy.rbac.entity.UserRole;
import com.zmxstudy.rbac.service.RoleService;
import jakarta.validation.constraints.NotEmpty;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author star
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<RoleService, Role> {
    @PutMapping("edit/auths")
    public boolean editRoles(@RequestHeader("roleId") Long roleId, @RequestBody List<Long> authIds) {
        return baseService.editRoles(roleId, authIds);
    }

    @GetMapping("/userAndRole")
    public List<UserAndRole> findUsernameRoleName(){
//        System.out.println(baseService.findUsernameAndRoleName());
        return baseService.findUsernameAndRoleName();

    }

    @GetMapping("/findByUsername")
    public List<UserAndRole> findByUsername(@RequestParam(value = "username", required = false) String username){
//        System.out.println(username);

       return baseService.findUserAndRoleByUsername(username);
    }

    @DeleteMapping("/delUserAndRole")
    public boolean del(@RequestHeader("User-Id") @NotEmpty int userId, @RequestHeader("Role-Id") @NotEmpty int roleId){
        if(baseService.delUserAndRole(userId,roleId)){
            return true;
        }
        return false;
    }

    @PostMapping("/plusRole")
    public boolean plus(@RequestBody UserRole userRole){
        int userId = userRole.getUserId();
        int roleId = userRole.getRoleId();
        if(baseService.plusRole(userId,roleId)){
            return true;
        }
        return false;
    }
}
